﻿using log4net;
using Mangox.Common;
using Mangox.Common.Tookit;
using Mangox.Model.Common;
using Mangox.Model.MangoxDB;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Mangox.BLL.Mangox
{
    public class ShuiLongTouBLL
    {
        protected static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static string className = (new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name + "/";


        /// <summary>
        /// 添加水龙头点击记录
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool AddShuiLongTou(S_ShuiLongTou param)
        {
            var flag = false;
            try
            {
                log.Info($"添加水龙头活动点击-{JsonCommon.ToJson(param)}");

                using (MangoxDBEntities dbContext = new MangoxDBEntities())
                {
                    var temp = new S_ShuiLongTou();
                    ObjectReflection.AutoMapping(param, temp);

                    dbContext.S_ShuiLongTou.Add(temp);
                    flag = dbContext.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                log.Error(className + $"添加水龙头失败 param-{JsonCommon.ToJson(param)} fail-{ex}");
                throw ex;
            }
            return flag;
        }


        # region 统计方法

        /// <summary>
        /// 查询用户总数
        /// </summary>
        /// <returns></returns>
        public int GetUserCount()
        {
            try
            {
                using (MangoxDBEntities dbContext = new MangoxDBEntities())
                {
                    //查询点击日志，再EF去重，EF去重复
                    //var count = dbContext.S_ShuiLongTou.Select(p => p.UserID).Distinct().Count();

                    //查询user表
                    var count = dbContext.M_User.Where(p => p.State == (int)UserStatus.Enabled && p.AppType == (int)AppType.ShuiLongTou).Count();
                    return count;
                }
            }
            catch (Exception ex)
            {
                //log.Error(className + $"GetUserCount-{openid} 登陆失败 fail-{ex}");
                throw ex;
            }
        }

        /// <summary>
        /// 查询总点击数
        /// </summary>
        /// <returns></returns>
        public long GetTouchCount()
        {
            try
            {
                using (MangoxDBEntities dbContext = new MangoxDBEntities())
                {
                    var count = dbContext.S_ShuiLongTou.Count();
                    return count;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// *统计当日所有用户点击次数按日期分组
        /// 获取奖励
        /// </summary>
        /// <returns></returns>
        public List<RewardCountResult> GetRewardCountList(int userid)
        {
            //在mysql里面,如果没有影响,那么返回行数为  -1 ,sqlserver 里面  还没有测试过
            //ctx.Database.ExecuteSqlCommand("UPDATE Person SET Name = 'Michael' WHERE PersonID = 1");

            var result = new List<RewardCountResult>();

            try
            {
                //select TOP 10
                //CONVERT(varchar(10), DATEADD(hour, 12, createtime), 102) as '点击日期',
                //COUNT(*) as '每日次数'
                //from S_ShuiLongTou
                //-- where userid = '6457221'
                //group by
                //CONVERT(varchar(10), DATEADD(hour, 12, createtime), 102) ORDER BY '点击日期' DESC


                using (MangoxDBEntities dbContext = new MangoxDBEntities())
                {
                    var model = new { };

                    var sql = @"select TOP 10
                        CONVERT(varchar(10),DATEADD(hour,12,createtime),102) as 'TouchTime',
                        COUNT(*)as 'TouchCount'
                        from S_ShuiLongTou
                        -- where userid=''
                        group by 
                        CONVERT(varchar(10),DATEADD(hour,12,createtime),102) ORDER BY 'TouchTime' DESC";

                    result = dbContext.Database.SqlQuery<RewardCountResult>(sql).ToList();
                }
            }
            catch (Exception ex)
            {
                result = null;
                throw ex;
            }
            return result;
        }


        #endregion




    }
}
